package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Commodity;
import com.example.demo.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-23
 */
@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityMapper commodityMapper;

    @GetMapping(value = "/getCommodityList")
    @ResponseBody
    // 获取所有的商品
    public String getCommodityList()
    {
        List<Commodity> commodities = commodityMapper.selectList(null);
        return JSON.toJSONString(commodities);
    }

    @GetMapping(value = "/getOneCommodity")
    @ResponseBody
    // 获取某种商品
    public String getOneCommodity(String id)
    {
        System.out.println(id);
        QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
        commodityQueryWrapper.eq("id",id);
        Commodity commodity = commodityMapper.selectOne(commodityQueryWrapper);
        return JSON.toJSONString(commodity);
    }

}

